home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / _open.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  3KB  |  148 lines

  1. RCS_ID_C="$Id: _open.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _open.c - Unix compatible open() (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <dos/dos.h>
  17. #include <proto/dos.h>
  18. #include <proto/usergroup.h>
  19. #include <stdarg.h>
  20. #include <unistd.h>
  21.  
  22. #include <bsdsocket.h>
  23.  
  24. #include "netlib.h"
  25.  
  26. extern int (*__closefunc)(int);
  27.  
  28. __stdargs int
  29. __open(const char *name, int mode, ...)
  30. {
  31.   struct UFB *ufb;
  32.   int         fd;
  33.   int         flags;
  34.   char        newfile = TRUE;
  35.   BPTR        file;
  36.  
  37.   /*
  38.    * Set up __closefunc (which is used at cleanup)
  39.    */
  40.   __closefunc = __close;
  41.  
  42.   /*
  43.    * Check for the break signals
  44.    */
  45.   __chkabort();
  46.  
  47.   /*
  48.    * find first free ufb
  49.    */
  50.   ufb = __allocufb(&fd);
  51.   if (ufb == NULL)
  52.     return -1; /* errno is set by the __allocufb() */
  53.  
  54.   /*
  55.    * malloc space for the name & copy it
  56.    */
  57.   if ((ufb->ufbfn = malloc(strlen(name)+1)) == NULL) {
  58.     SET_OSERR(ERROR_NO_FREE_STORE);
  59.     errno = ENOMEM;
  60.     return -1;
  61.   }
  62.   strcpy(ufb->ufbfn, name);
  63.   /*
  64.    * Translate mode to ufb flags
  65.    */
  66.   switch (mode & (O_WRONLY | O_RDWR)) {
  67.   case O_RDONLY:
  68.     if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
  69.       errno = EINVAL;
  70.       return -1;
  71.     }
  72.     flags = UFB_RA;
  73.     break;
  74.   case O_WRONLY:
  75.     flags = UFB_WA;
  76.     break;
  77.   case O_RDWR:
  78.     flags = UFB_RA | UFB_WA;
  79.     break;
  80.   default:
  81.     errno = EINVAL;
  82.     return -1;
  83.   }
  84.   if (mode & O_APPEND)
  85.     flags |= UFB_APP;
  86.   if (mode & O_XLATE)
  87.     flags |= UFB_XLAT;
  88.   if (mode & O_TEMP)
  89.     flags |= UFB_TEMP;
  90.   if (mode & O_CREAT) {
  91.     BPTR lock;
  92.     if (lock = Lock((char *)name, SHARED_LOCK)) {
  93.       if (mode & O_EXCL) {
  94.     UnLock(lock);
  95.     errno = EEXIST;
  96.     free(ufb->ufbfn);
  97.     return -1;
  98.       }
  99.  
  100.       if (mode & O_TRUNC)
  101.     newfile = FALSE;
  102.       else
  103.     mode &= ~O_CREAT;
  104.  
  105.       UnLock(lock);
  106.     }
  107.   }
  108.   if (mode & O_CREAT) {
  109.     if ((file = Open((char *)name, MODE_NEWFILE)) == NULL)
  110.       goto osfail;
  111.  
  112.     if (newfile) {
  113.       va_list va;
  114.       int cmode;
  115.  
  116.       va_start(va, mode);
  117.  
  118.       cmode = va_arg(va, int) & ~getumask();
  119.       
  120.       chmod((char *)name, cmode); /* hope this doesn't fail :-) */
  121.     }
  122.   }
  123.   else {
  124.     if ((file = Open((char *)name,
  125.              (flags & UFB_WA && mode & O_LOCK) ? 
  126.              MODE_READWRITE : MODE_OLDFILE)) == NULL)
  127.       goto osfail;
  128.   }
  129.  
  130.   /*
  131.    * All done! Setting the ufb->ufbflg field to non-zero value marks
  132.    * this ufb used. 
  133.    */
  134.   ufb->ufbflg = flags;
  135.   ufb->ufbfh = (long)file;
  136.  
  137.   return fd;
  138.  
  139. osfail:
  140.   {
  141.     int code = IoErr();
  142.     if (ufb->ufbfn)
  143.       free(ufb->ufbfn);
  144.     set_errno(code);
  145.   }
  146.   return -1;
  147. }
  148.